package com.siqiaoren.controller;

import com.siqiaoren.domain.User;
import com.siqiaoren.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @CrossOrigin
    @GetMapping("/save")
    @ResponseBody
    private Boolean save(HttpServletRequest request, HttpServletResponse response){
//        response.setHeader("Access-Control-Allow-Origin", "http://www.siqiaoren.top:81");
//        System.out.println(user);
//        return userService.save(user);
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user.setPhone(request.getParameter("phone"));
//        System.out.println(user);
        boolean aa = userService.save(user);
        System.out.println(aa);
        return aa;
    }

    @CrossOrigin
    @GetMapping("/check")
    @ResponseBody
    private Boolean check(HttpServletRequest request, HttpServletResponse response){
//        System.out.println(user);
//        return userService.save(user);
//        response.setHeader("Access-Control-Allow-Origin", "http://www.siqiaoren.top:81");
        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        System.out.println(user);
        return userService.checkNamePassword(request.getParameter("username"), request.getParameter("password"));
    }
}
